﻿using System.Collections.Generic;
using System;
using CRC.Clases;
using NHibernate;
using CRC.NHibernate;
using CRC.Incidencias.Dominio.Clases;
using CRC.GestionSalones.Dominio.Clases;

namespace CRC.GestionSalones.Controladores
{
    class ControladorListarSalones
    {
        private static String strQrySalones = 
            "select s.Id, s.Proyecto, s.Nombre_Salon, s.Telefono, " +
            "s.Direccion, s.CP, munic.Nombre1 " +
            "from Salon s " +
            "left join s.Municipio as munic " +
            "order by s.Nombre_Salon";

        private static String strQrySalonesLigeros = "select s.Id, s.Nombre_Salon from Salon s order by s.Nombre_Salon";

        public IList<Object[]> listarSalones()
        {
            ISession session = null;
            ITransaction tx = null;
            IList<Object[]> salones;

            try
            {
                session = NHibernateHelper.GetCurrentSession();
                tx = session.BeginTransaction();

                salones = session.CreateQuery(strQrySalones).List<Object[]>();

                tx.Commit();
                return salones;
            }
            catch (HibernateException ex)
            {
                if (tx != null) { tx.Rollback(); }
                if (session != null) session.Close();
                throw ex;
            }
        }

        public IList<Object[]> listarSalonesLigeros()
        {
            ISession session = null;
            IList<Object[]> salones = null;

            try
            {
                session = NHibernateHelper.GetCurrentSession();
                salones = session.CreateQuery(strQrySalonesLigeros).List<Object[]>();

                return salones;
            }
            catch (HibernateException ex)
            {
                if (session != null) session.Close();
                throw ex;
            }
        }
    }
}
